{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ZjRn1KazJQqn"
      },
      "source": [
        "Copyright 2020 Google LLC.\n",
        "\n",
        "Licensed aunder the Apache License, Version 2.0 (the \"License\");"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "wHZQpP65JGvj"
      },
      "outputs": [],
      "source": [
        "#@title License\n",
        "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Y2E55nYzg46t"
      },
      "source": [
        "# Get Generative AI API Key\n",
        "\n",
        "Use the links below to get your API key!\n",
        "\n",
        "* https://developers.generativeai.google/tutorials/setup\n",
        "\n",
        "* https://makersuite.google.com/app/apikey\n",
        "\n",
        "\n",
        "Then go to \"Tools\" \u003e \"Command Palette\" and search for \"secrets\" and choose to\n",
        "\"Open user secrets tab\" and save your API key in a variable e.g. genAI-api-key.\n",
        "\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "1MduR0vtf_qp"
      },
      "source": [
        "## Installs\n",
        "\n",
        "NOTE: You will need to \"Restart kernel\" once the installations are complete before you proceed."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "xiDWvyORM_wi"
      },
      "outputs": [],
      "source": [
        "!pip install -Iv pydantic==1.10.2\n",
        "!pip install matminer\n",
        "!pip install google-ai-generativelanguage\n",
        "!pip install google-generativeai\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "xIop9Fx1hBfH"
      },
      "outputs": [],
      "source": [
        "#@title read API key saved in notebook secrets tab.\n",
        "from google.colab import userdata\n",
        "YOUR_API_KEY = genai.configure(api_key=userdata.get('genAI-api-key'))\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "executionInfo": {
          "elapsed": 7713,
          "status": "ok",
          "timestamp": 1699048498782,
          "user": {
            "displayName": "Subhashini Venugopalan",
            "userId": "04553118295578179219"
          },
          "user_tz": 420
        },
        "id": "VYaePHWSNA8y",
        "outputId": "a793b9ba-9bbd-42c9-8055-a553a373566d"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "models/chat-bison-001\n",
            "models/text-bison-001\n",
            "models/embedding-gecko-001\n"
          ]
        }
      ],
      "source": [
        "#@title import and check the available models using your API key.\n",
        "import os\n",
        "import re\n",
        "import google.generativeai as genai\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "import ast\n",
        "from matminer.datasets.convenience_loaders import load_expt_gap\n",
        "from matminer.featurizers.composition import ElementProperty\n",
        "from pymatgen.core import Composition # we will use this to parse formulas in the dataset\n",
        "\n",
        "\n",
        "genai.configure(api_key=YOUR_API_KEY)\n",
        "for model in genai.list_models():\n",
        "  print(model.name)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 86,
          "referenced_widgets": [
            "d219cec09a7f4f66aa013b0fa5fd32da",
            "0220be296b814b1b985009198288928f",
            "38109e2f2ac44b059088d4129a2c4b46",
            "edb65d5fff13453bba473ade55da3f82",
            "142bb546c6cd48c5836aad8c9ac94296",
            "bb304ab65ce1470ba7c0e0172e994291",
            "662add7b65fc469cba2261d904c0655e",
            "6a81583f9ee44b219aa06d7352f6e494",
            "63dd9738f1b5435397705fb22b1a9fc6",
            "60173dac06bc461eaa9020330725e968",
            "e3c44dc313494373a914cae36dfa649b"
          ]
        },
        "executionInfo": {
          "elapsed": 56615,
          "status": "ok",
          "timestamp": 1699048576014,
          "user": {
            "displayName": "Subhashini Venugopalan",
            "userId": "04553118295578179219"
          },
          "user_tz": 420
        },
        "id": "yppz6mnjLISi",
        "outputId": "273ff27d-cb97-40d7-a35d-932c89d4c297"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Fetching expt_gap.json.gz from https://ndownloader.figshare.com/files/13464434 to /usr/local/lib/python3.10/dist-packages/matminer/datasets/expt_gap.json.gz\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "Fetching https://ndownloader.figshare.com/files/13464434 in MB: 0.051199999999999996MB [00:00, 15.02MB/s]     \n"
          ]
        },
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "d219cec09a7f4f66aa013b0fa5fd32da",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "ElementProperty:   0%|          | 0/6353 [00:00\u003c?, ?it/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "#@title download the matbench dataset\n",
        "\n",
        "bandgap_dataset = load_expt_gap() # loads the experimental bandgap dataset\n",
        "# Convert the formulas to pymatgen composition objects\n",
        "bandgap_dataset['composition'] = [Composition(formula) for formula in bandgap_dataset['formula']]\n",
        "\n",
        "# Check if compositions are valid\n",
        "invalid_compositions = [composition for composition in bandgap_dataset['composition'] if not composition.valid]\n",
        "\n",
        "# Drop rows with invalid compositions\n",
        "bandgap_dataset = bandgap_dataset.drop(bandgap_dataset[bandgap_dataset['composition'].isin(invalid_compositions)].index)\n",
        "\n",
        "# Print number of dropped rows\n",
        "# print(f'Dropped {len(invalid_compositions)} rows with invalid compositions.')\n",
        "element_featurizer = ElementProperty.from_preset(preset_name=\"magpie\")\n",
        "bandgap_dataset = element_featurizer.featurize_dataframe(bandgap_dataset, col_id = 'composition', )\n",
        "bandgap_dataset = bandgap_dataset[~ bandgap_dataset.isnull().any(axis=1)] # drop rows where we got a nan, in case any.\n",
        "\n",
        "bandgap_dataset.to_csv('expt_bandgap_w_magpie.csv')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "WvgGUgXxq20Q"
      },
      "outputs": [],
      "source": [
        "#@title Helpers to query the model and parse responses\n",
        "\n",
        "def query_model(query_prompt: str, model_name: str) -\u003e str:\n",
        "  response = genai.generate_text(\n",
        "    model=model_name,\n",
        "    prompt=query_prompt,\n",
        "    candidate_count=1,\n",
        "    temperature=0.0,\n",
        "  )\n",
        "  return response.result\n",
        "\n",
        "def write_code_for_task(task_description: str,\n",
        "                        parsed: bool = False,\n",
        "                        parse_multiple: bool = False,\n",
        "                        verbose: bool = True) -\u003e str:\n",
        "  response = query_model(task_description, 'models/text-bison-001')\n",
        "  if verbose:\n",
        "    print(response)\n",
        "  if parsed:\n",
        "      parsed_block = response.split('python\\n')[1]\n",
        "      code_block = parsed_block.split('```')[0]\n",
        "      if verbose:\n",
        "        print(code_block)\n",
        "      return code_block\n",
        "  if parse_multiple:\n",
        "    multi_code_blocks = []\n",
        "    for py_code in response.split('python\\n')[1:]:\n",
        "      multi_code_blocks.append(py_code.split('```')[0])\n",
        "    return \"\\n\".join(multi_code_blocks)\n",
        "  return response\n",
        "\n",
        "ok_code_registry = {\n",
        "    'def load_csv(filename: str) -\u003e pd.DataFrame:\\n  \"\"\"Load a .csv file into'\n",
        "    ' pandas.\"\"\"\\n  \\n  df = pd.read_csv(filename)\\n  return df\\n'\n",
        "}\n",
        "bad_code_registry = set()\n",
        "\n",
        "def local_exec(code):\n",
        "  return exec(code, globals(), locals())\n",
        "\n",
        "def exec_and_register_if_ok(body, env_exec, code_conv_ctx=[]):\n",
        "  \"\"\"Call local_exec on body iff it is registered as ok or the response is 'y'.\"\"\"\n",
        "  # Mutates module-level variables as a side-effect:\n",
        "  #   ok_code_registry, bad_code_registry\n",
        "  do_exec = False\n",
        "  if body in ok_code_registry:\n",
        "    do_exec = True\n",
        "  elif body in bad_code_registry:\n",
        "    print('Nope!')\n",
        "  else:\n",
        "    print('###BEGIN###')\n",
        "    print(body)\n",
        "    print('###END###')\n",
        "    response = input('Is the preceding code ok? (y/n)').lower()\n",
        "    if response == 'y':\n",
        "      ok_code_registry.add(body)\n",
        "      do_exec = True\n",
        "    elif response == 'n':\n",
        "      bad_code_registry.add(body)\n",
        "    else:\n",
        "      print(f'Unexpected response: {response} is ignored.')\n",
        "  if do_exec:\n",
        "    code_ctx = \"\\n\\n\".join(code_conv_ctx) + \"\\n\\n\" + body\n",
        "    value = env_exec(code_ctx)\n",
        "  else:\n",
        "    value = None\n",
        "  return do_exec, value\n",
        "\n",
        "def simple_code_interpreter(env_exec=local_exec):\n",
        "  stop = False\n",
        "  executed_codes = []\n",
        "  while not stop:\n",
        "    f_description = input('Describe what you want to do? Say stop-stop to end')\n",
        "    if f_description == 'stop-stop': return\n",
        "    predicted_f_code = write_code_for_task(f_description, parse_multiple=True)\n",
        "    _ = exec_and_register_if_ok(predicted_f_code, env_exec,\n",
        "                                code_conv_ctx=executed_codes)\n",
        "    executed_codes.append(predicted_f_code)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "UYUGJUWVUm2W"
      },
      "source": [
        "## Prompt LLM to load the data and train a model."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "executionInfo": {
          "elapsed": 125575,
          "status": "ok",
          "timestamp": 1699053314831,
          "user": {
            "displayName": "Subhashini Venugopalan",
            "userId": "04553118295578179219"
          },
          "user_tz": 420
        },
        "id": "0iq9PJdAI1fc",
        "outputId": "d9a64635-7050-4fdc-d512-6a604685ad5f"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "\n",
            "```python\n",
            "import pandas as pd\n",
            "from sklearn.model_selection import train_test_split, cross_val_score\n",
            "from sklearn.metrics import mean_absolute_error\n",
            "from xgboost import XGBRegressor\n",
            "from sklearn.linear_model import LinearRegression\n",
            "from sklearn.ensemble import RandomForestRegressor\n",
            "```\n",
            "\n",
            "Load the data and extract the feature labels.\n",
            "```python\n",
            "bandgap_dataset = pd.read_csv(\"expt_bandgap_w_magpie.csv\")\n",
            "feature_labels = bandgap_dataset.columns[4:]\n",
            "```\n",
            "\n",
            "Split the data into training and test sets.\n",
            "```python\n",
            "X_train, X_test, y_train, y_test = train_test_split(bandgap_dataset[feature_labels], bandgap_dataset[\"gap expt\"], test_size=0.2)\n",
            "```\n",
            "\n",
            "Train and evaluate XGBoost, Linear Regression and Random Forest models.\n",
            "```python\n",
            "# XGBoost\n",
            "xgboost_model = XGBRegressor()\n",
            "xgboost_scores = cross_val_score(xgboost_model, X_train, y_train, cv=5, scoring=\"neg_mean_absolute_error\")\n",
            "print(\"XGBoost mean absolute error:\", np.mean(xgboost_scores))\n",
            "\n",
            "# Linear Regression\n",
            "linear_regression_model = LinearRegression()\n",
            "linear_regression_scores = cross_val_score(linear_regression_model, X_train, y_train, cv=5, scoring=\"neg_mean_absolute_error\")\n",
            "print(\"Linear Regression mean absolute error:\", np.mean(linear_regression_scores))\n",
            "\n",
            "# Random Forest\n",
            "random_forest_model = RandomForestRegressor()\n",
            "random_forest_scores = cross_val_score(random_forest_model, X_train, y_train, cv=5, scoring=\"neg_mean_absolute_error\")\n",
            "print(\"Random Forest mean absolute error:\", np.mean(random_forest_scores))\n",
            "```\n",
            "\n",
            "Output:\n",
            "```\n",
            "XGBoost mean absolute error: -0.00011010000000000001\n",
            "Linear Regression mean absolute error: -0.00011010000000000001\n",
            "Random Forest mean absolute error: -0.00011010000000000001\n",
            "```\n",
            "###BEGIN###\n",
            "import pandas as pd\n",
            "from sklearn.model_selection import train_test_split, cross_val_score\n",
            "from sklearn.metrics import mean_absolute_error\n",
            "from xgboost import XGBRegressor\n",
            "from sklearn.linear_model import LinearRegression\n",
            "from sklearn.ensemble import RandomForestRegressor\n",
            "\n",
            "bandgap_dataset = pd.read_csv(\"expt_bandgap_w_magpie.csv\")\n",
            "feature_labels = bandgap_dataset.columns[4:]\n",
            "\n",
            "X_train, X_test, y_train, y_test = train_test_split(bandgap_dataset[feature_labels], bandgap_dataset[\"gap expt\"], test_size=0.2)\n",
            "\n",
            "# XGBoost\n",
            "xgboost_model = XGBRegressor()\n",
            "xgboost_scores = cross_val_score(xgboost_model, X_train, y_train, cv=5, scoring=\"neg_mean_absolute_error\")\n",
            "print(\"XGBoost mean absolute error:\", np.mean(xgboost_scores))\n",
            "\n",
            "# Linear Regression\n",
            "linear_regression_model = LinearRegression()\n",
            "linear_regression_scores = cross_val_score(linear_regression_model, X_train, y_train, cv=5, scoring=\"neg_mean_absolute_error\")\n",
            "print(\"Linear Regression mean absolute error:\", np.mean(linear_regression_scores))\n",
            "\n",
            "# Random Forest\n",
            "random_forest_model = RandomForestRegressor()\n",
            "random_forest_scores = cross_val_score(random_forest_model, X_train, y_train, cv=5, scoring=\"neg_mean_absolute_error\")\n",
            "print(\"Random Forest mean absolute error:\", np.mean(random_forest_scores))\n",
            "\n",
            "###END###\n",
            "Is the preceding code ok? (y/n)y\n",
            "XGBoost mean absolute error: -0.38276560258572506\n",
            "Linear Regression mean absolute error: -0.8456817104295006\n",
            "Random Forest mean absolute error: -0.38427504322033534\n"
          ]
        }
      ],
      "source": [
        "out6 = write_code_for_task(\"\"\"\n",
        " Load expt_bandgap_w_magpie.csv into a dataframe called bandgap_dataset.\n",
        " Except for the first 4 columns save the rest of the column\n",
        " names of the bandgap_dataset dataframe in the variable \"feature_labels\".\n",
        "\n",
        " Write code to train and evaluate XGBoost, Linear Regression and Random Forest models\n",
        " to predict the band gap (in column \"gap expt\") from the set of features (take only the columns in \"feature_labels\")?\n",
        " Do 5-fold cross validation and use negative mean absolute error for scoring.\n",
        " Import necessary libraries.\"\"\",\n",
        "                           parse_multiple=True,\n",
        "                           verbose=True)\n",
        "\n",
        "_ = exec_and_register_if_ok(out6, local_exec)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "zMBsa6sip8y4"
      },
      "source": [
        "## Try out different prompts"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "qPsmSBBsi5Pt"
      },
      "outputs": [],
      "source": [
        "# @title Check code generation for different prompts for loading data and model evaluation.\n",
        "out1 = write_code_for_task(\n",
        "    'Write python code to load a csv file into a dataframe',\n",
        "    parsed=True, verbose=True)\n",
        "\n",
        "out2 = write_code_for_task(\"\"\"Write python code to Load file\n",
        " expt_bandgap_w_magpie.csv into a pandas dataframe called bandgap_dataset.\n",
        " print head of the dataframe\"\"\",\n",
        "                           parsed=True,\n",
        "                           verbose=True)\n",
        "\n",
        "out3 = write_code_for_task(\"\"\"Except for the first 4 columns save the rest of the column\n",
        "names of the bandgap_dataset dataframe in the variable \"feature_labels\"\n",
        "print first 5 items of feature_labels. ```\"\"\",\n",
        "                           parsed=True,\n",
        "                           verbose=True)\n",
        "\n",
        "out4 = write_code_for_task(\"\"\"You have  a dataframe called\n",
        " bandgap_dataset. It has columns \"formula\" and \"gap expt\", and several columns\n",
        " with features the feature columns are saved in the variable \"feature_labels\".\n",
        " Write code to train and evaluate XGBoost, Linear Regression and Random Forest models\n",
        " to predict the band gap (\"gap expt\") from the features (only take only the columns in \"feature_labels\")?\n",
        " Use negative mean absolute error for scoring. Import necessary libraries.\"\"\",\n",
        "                           parsed=True,\n",
        "                           verbose=True)\n",
        "\n",
        "out5 = write_code_for_task(\"\"\"\n",
        " Load expt_bandgap_w_magpie.csv into a dataframe called bandgap_dataset.\n",
        " Except for the first 4 columns save the rest of the column\n",
        " names of the bandgap_dataset dataframe in the variable \"feature_labels\"\n",
        " Write code to train and evaluate XGBoost, Linear Regression and Random Forest models\n",
        " to predict the band gap (in column \"gap expt\") from the set of features (take only the columns in \"feature_labels\")?\n",
        " Use negative mean absolute error for scoring. Import necessary libraries.\"\"\",\n",
        "                           parsed=True,\n",
        "                           verbose=True)\n",
        "\n",
        "out6 = write_code_for_task(\"\"\"\n",
        " Load expt_bandgap_w_magpie.csv into a dataframe called bandgap_dataset.\n",
        " Except for the first 4 columns save the rest of the column\n",
        " names of the bandgap_dataset dataframe in the variable \"feature_labels\".\n",
        "\n",
        " Write code to train and evaluate XGBoost, Linear Regression and Random Forest models\n",
        " to predict the band gap (in column \"gap expt\") from the set of features (take only the columns in \"feature_labels\")?\n",
        " Do 5-fold cross validation and use negative mean absolute error for scoring.\n",
        " Import necessary libraries.\"\"\",\n",
        "                           parse_multiple=True,\n",
        "                           verbose=True)\n",
        "\n",
        "output_responses = [out1, out2, out3, out4, out5, out6]\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "f2i4IoD-hBZb"
      },
      "outputs": [],
      "source": [
        "#@title Execute single prompt to train and compare models with 5-fold cross validation\n",
        "_ = exec_and_register_if_ok(out6, my_exec)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "executionInfo": {
          "elapsed": 107734,
          "status": "ok",
          "timestamp": 1698819961757,
          "user": {
            "displayName": "Subhashini Venugopalan",
            "userId": "04553118295578179219"
          },
          "user_tz": 420
        },
        "id": "JxZZlx6zjcDW",
        "outputId": "a568db6b-bcc2-47ae-a977-01fa942ea0b5"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "XGBoost MAE: -0.4289969593568488\n",
            "Linear Regression MAE: -42.91331668878381\n",
            "Random Forest MAE: -0.438315558836152\n"
          ]
        }
      ],
      "source": [
        "_ = exec_and_register_if_ok(out6, local_exec)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "executionInfo": {
          "elapsed": 28703,
          "status": "ok",
          "timestamp": 1698818333635,
          "user": {
            "displayName": "Subhashini Venugopalan",
            "userId": "04553118295578179219"
          },
          "user_tz": 420
        },
        "id": "-CMo1D4MdZ3o",
        "outputId": "b8784855-bdcf-49b8-995e-0072aecd52d9"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "###BEGIN###\n",
            "import pandas as pd\n",
            "from sklearn.model_selection import train_test_split\n",
            "from sklearn.metrics import mean_absolute_error\n",
            "from xgboost import XGBRegressor\n",
            "from sklearn.linear_model import LinearRegression\n",
            "from sklearn.ensemble import RandomForestRegressor\n",
            "\n",
            "# Load the data\n",
            "bandgap_dataset = pd.read_csv(\"expt_bandgap_w_magpie.csv\")\n",
            "\n",
            "# Save the feature labels\n",
            "feature_labels = bandgap_dataset.columns[4:]\n",
            "\n",
            "# Split the data into training and test sets\n",
            "X_train, X_test, y_train, y_test = train_test_split(\n",
            "    bandgap_dataset[feature_labels], bandgap_dataset[\"gap expt\"], test_size=0.2\n",
            ")\n",
            "\n",
            "# Train the XGBoost model\n",
            "xgboost_model = XGBRegressor()\n",
            "xgboost_model.fit(X_train, y_train)\n",
            "\n",
            "# Train the Linear Regression model\n",
            "linear_regression_model = LinearRegression()\n",
            "linear_regression_model.fit(X_train, y_train)\n",
            "\n",
            "# Train the Random Forest model\n",
            "random_forest_model = RandomForestRegressor()\n",
            "random_forest_model.fit(X_train, y_train)\n",
            "\n",
            "# Evaluate the models\n",
            "xgboost_mae = mean_absolute_error(y_test, xgboost_model.predict(X_test))\n",
            "linear_regression_mae = mean_absolute_error(y_test, linear_regression_model.predict(X_test))\n",
            "random_forest_mae = mean_absolute_error(y_test, random_forest_model.predict(X_test))\n",
            "\n",
            "print(\"XGBoost MAE:\", xgboost_mae)\n",
            "print(\"Linear Regression MAE:\", linear_regression_mae)\n",
            "print(\"Random Forest MAE:\", random_forest_mae)\n",
            "\n",
            "###END###\n",
            "Is the preceding code ok? (y/n)y\n",
            "XGBoost MAE: 0.3708525548004474\n",
            "Linear Regression MAE: 0.9479265544343054\n",
            "Random Forest MAE: 0.37212517565283876\n"
          ]
        }
      ],
      "source": [
        "#@title Execute training XGBoost, LinearRegression and RandomForest models.\n",
        "_ = exec_and_register_if_ok(out5, local_exec)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "S_Q-yb0_qOSf"
      },
      "source": [
        "## Execute prompts in a code-interpretor-style step by step"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "mgSWfbgxZPmu"
      },
      "outputs": [],
      "source": [
        "#@title Do it step by step using multiple prompts.\n",
        "\"\"\"Write python code to Load file\n",
        " expt_bandgap_w_magpie.csv into a pandas dataframe called bandgap_dataset.\n",
        " print head of the dataframe\"\"\"\n",
        "\n",
        "\"\"\"Except for the first 4 columns save the rest of the column\n",
        "names of the bandgap_dataset dataframe in the variable \"feature_labels\"\n",
        "print first 5 items of feature_labels. ```\"\"\"\n",
        "\n",
        "\"\"\"You have  a dataframe called bandgap_dataset it was loaded from file expt_bandgap_w_magpie.csv.\n",
        " It has columns \"formula\" and \"gap expt\", and several columns\n",
        " with features. Except for the first 4 columns save the rest of the column\n",
        "  names of the bandgap_dataset dataframe in the variable \"feature_labels\".\n",
        " Write code to train and evaluate XGBoost, Linear Regression and Random Forest models\n",
        " to predict the band gap (\"gap expt\") from the features (only take only the columns in \"feature_labels\")?\n",
        " Use negative mean absolute error for scoring. Import necessary libraries.\"\"\"\n",
        "\n",
        "simple_code_interpreter(env_exec=local_exec)"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": [
        {
          "file_id": "1fXcQPxN-VWWaRGSI6C_tgVJYn6n-5A9W",
          "timestamp": 1699055360074
        },
        {
          "file_id": "1-bNS0jP4bghovdo4HMydM088Erzq8J10",
          "timestamp": 1698857784515
        }
      ],
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    },
    "widgets": {
      "application/vnd.jupyter.widget-state+json": {
        "0220be296b814b1b985009198288928f": {
          "model_module": "@jupyter-widgets/controls",
          "model_module_version": "1.5.0",
          "model_name": "HTMLModel",
          "state": {
            "_dom_classes": [],
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "HTMLModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/controls",
            "_view_module_version": "1.5.0",
            "_view_name": "HTMLView",
            "description": "",
            "description_tooltip": null,
            "layout": "IPY_MODEL_bb304ab65ce1470ba7c0e0172e994291",
            "placeholder": "​",
            "style": "IPY_MODEL_662add7b65fc469cba2261d904c0655e",
            "value": "ElementProperty: 100%"
          }
        },
        "142bb546c6cd48c5836aad8c9ac94296": {
          "model_module": "@jupyter-widgets/base",
          "model_module_version": "1.2.0",
          "model_name": "LayoutModel",
          "state": {
            "_model_module": "@jupyter-widgets/base",
            "_model_module_version": "1.2.0",
            "_model_name": "LayoutModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "LayoutView",
            "align_content": null,
            "align_items": null,
            "align_self": null,
            "border": null,
            "bottom": null,
            "display": null,
            "flex": null,
            "flex_flow": null,
            "grid_area": null,
            "grid_auto_columns": null,
            "grid_auto_flow": null,
            "grid_auto_rows": null,
            "grid_column": null,
            "grid_gap": null,
            "grid_row": null,
            "grid_template_areas": null,
            "grid_template_columns": null,
            "grid_template_rows": null,
            "height": null,
            "justify_content": null,
            "justify_items": null,
            "left": null,
            "margin": null,
            "max_height": null,
            "max_width": null,
            "min_height": null,
            "min_width": null,
            "object_fit": null,
            "object_position": null,
            "order": null,
            "overflow": null,
            "overflow_x": null,
            "overflow_y": null,
            "padding": null,
            "right": null,
            "top": null,
            "visibility": null,
            "width": null
          }
        },
        "38109e2f2ac44b059088d4129a2c4b46": {
          "model_module": "@jupyter-widgets/controls",
          "model_module_version": "1.5.0",
          "model_name": "FloatProgressModel",
          "state": {
            "_dom_classes": [],
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "FloatProgressModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/controls",
            "_view_module_version": "1.5.0",
            "_view_name": "ProgressView",
            "bar_style": "success",
            "description": "",
            "description_tooltip": null,
            "layout": "IPY_MODEL_6a81583f9ee44b219aa06d7352f6e494",
            "max": 6353,
            "min": 0,
            "orientation": "horizontal",
            "style": "IPY_MODEL_63dd9738f1b5435397705fb22b1a9fc6",
            "value": 6353
          }
        },
        "60173dac06bc461eaa9020330725e968": {
          "model_module": "@jupyter-widgets/base",
          "model_module_version": "1.2.0",
          "model_name": "LayoutModel",
          "state": {
            "_model_module": "@jupyter-widgets/base",
            "_model_module_version": "1.2.0",
            "_model_name": "LayoutModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "LayoutView",
            "align_content": null,
            "align_items": null,
            "align_self": null,
            "border": null,
            "bottom": null,
            "display": null,
            "flex": null,
            "flex_flow": null,
            "grid_area": null,
            "grid_auto_columns": null,
            "grid_auto_flow": null,
            "grid_auto_rows": null,
            "grid_column": null,
            "grid_gap": null,
            "grid_row": null,
            "grid_template_areas": null,
            "grid_template_columns": null,
            "grid_template_rows": null,
            "height": null,
            "justify_content": null,
            "justify_items": null,
            "left": null,
            "margin": null,
            "max_height": null,
            "max_width": null,
            "min_height": null,
            "min_width": null,
            "object_fit": null,
            "object_position": null,
            "order": null,
            "overflow": null,
            "overflow_x": null,
            "overflow_y": null,
            "padding": null,
            "right": null,
            "top": null,
            "visibility": null,
            "width": null
          }
        },
        "63dd9738f1b5435397705fb22b1a9fc6": {
          "model_module": "@jupyter-widgets/controls",
          "model_module_version": "1.5.0",
          "model_name": "ProgressStyleModel",
          "state": {
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "ProgressStyleModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "StyleView",
            "bar_color": null,
            "description_width": ""
          }
        },
        "662add7b65fc469cba2261d904c0655e": {
          "model_module": "@jupyter-widgets/controls",
          "model_module_version": "1.5.0",
          "model_name": "DescriptionStyleModel",
          "state": {
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "DescriptionStyleModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "StyleView",
            "description_width": ""
          }
        },
        "6a81583f9ee44b219aa06d7352f6e494": {
          "model_module": "@jupyter-widgets/base",
          "model_module_version": "1.2.0",
          "model_name": "LayoutModel",
          "state": {
            "_model_module": "@jupyter-widgets/base",
            "_model_module_version": "1.2.0",
            "_model_name": "LayoutModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "LayoutView",
            "align_content": null,
            "align_items": null,
            "align_self": null,
            "border": null,
            "bottom": null,
            "display": null,
            "flex": null,
            "flex_flow": null,
            "grid_area": null,
            "grid_auto_columns": null,
            "grid_auto_flow": null,
            "grid_auto_rows": null,
            "grid_column": null,
            "grid_gap": null,
            "grid_row": null,
            "grid_template_areas": null,
            "grid_template_columns": null,
            "grid_template_rows": null,
            "height": null,
            "justify_content": null,
            "justify_items": null,
            "left": null,
            "margin": null,
            "max_height": null,
            "max_width": null,
            "min_height": null,
            "min_width": null,
            "object_fit": null,
            "object_position": null,
            "order": null,
            "overflow": null,
            "overflow_x": null,
            "overflow_y": null,
            "padding": null,
            "right": null,
            "top": null,
            "visibility": null,
            "width": null
          }
        },
        "bb304ab65ce1470ba7c0e0172e994291": {
          "model_module": "@jupyter-widgets/base",
          "model_module_version": "1.2.0",
          "model_name": "LayoutModel",
          "state": {
            "_model_module": "@jupyter-widgets/base",
            "_model_module_version": "1.2.0",
            "_model_name": "LayoutModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "LayoutView",
            "align_content": null,
            "align_items": null,
            "align_self": null,
            "border": null,
            "bottom": null,
            "display": null,
            "flex": null,
            "flex_flow": null,
            "grid_area": null,
            "grid_auto_columns": null,
            "grid_auto_flow": null,
            "grid_auto_rows": null,
            "grid_column": null,
            "grid_gap": null,
            "grid_row": null,
            "grid_template_areas": null,
            "grid_template_columns": null,
            "grid_template_rows": null,
            "height": null,
            "justify_content": null,
            "justify_items": null,
            "left": null,
            "margin": null,
            "max_height": null,
            "max_width": null,
            "min_height": null,
            "min_width": null,
            "object_fit": null,
            "object_position": null,
            "order": null,
            "overflow": null,
            "overflow_x": null,
            "overflow_y": null,
            "padding": null,
            "right": null,
            "top": null,
            "visibility": null,
            "width": null
          }
        },
        "d219cec09a7f4f66aa013b0fa5fd32da": {
          "model_module": "@jupyter-widgets/controls",
          "model_module_version": "1.5.0",
          "model_name": "HBoxModel",
          "state": {
            "_dom_classes": [],
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "HBoxModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/controls",
            "_view_module_version": "1.5.0",
            "_view_name": "HBoxView",
            "box_style": "",
            "children": [
              "IPY_MODEL_0220be296b814b1b985009198288928f",
              "IPY_MODEL_38109e2f2ac44b059088d4129a2c4b46",
              "IPY_MODEL_edb65d5fff13453bba473ade55da3f82"
            ],
            "layout": "IPY_MODEL_142bb546c6cd48c5836aad8c9ac94296"
          }
        },
        "e3c44dc313494373a914cae36dfa649b": {
          "model_module": "@jupyter-widgets/controls",
          "model_module_version": "1.5.0",
          "model_name": "DescriptionStyleModel",
          "state": {
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "DescriptionStyleModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "StyleView",
            "description_width": ""
          }
        },
        "edb65d5fff13453bba473ade55da3f82": {
          "model_module": "@jupyter-widgets/controls",
          "model_module_version": "1.5.0",
          "model_name": "HTMLModel",
          "state": {
            "_dom_classes": [],
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "HTMLModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/controls",
            "_view_module_version": "1.5.0",
            "_view_name": "HTMLView",
            "description": "",
            "description_tooltip": null,
            "layout": "IPY_MODEL_60173dac06bc461eaa9020330725e968",
            "placeholder": "​",
            "style": "IPY_MODEL_e3c44dc313494373a914cae36dfa649b",
            "value": " 6353/6353 [00:53\u0026lt;00:00, 143.18it/s]"
          }
        }
      }
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
